/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.applicatioN.controllers;

import com.applicatioN.controllers.exceptions.NonexistentEntityException;
import com.applicatioN.controllers.exceptions.PreexistingEntityException;
import com.applicatioN.entities.MaitriserSoft;
import com.applicatioN.entities.MaitriserSoftPK;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import com.applicatioN.entities.Software;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Asus_k53s
 */
public class MaitriserSoftJpaController implements Serializable {

    public MaitriserSoftJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(MaitriserSoft maitriserSoft) throws PreexistingEntityException, Exception {
        if (maitriserSoft.getMaitriserSoftPK() == null) {
            maitriserSoft.setMaitriserSoftPK(new MaitriserSoftPK());
        }
        maitriserSoft.getMaitriserSoftPK().setIdentifiant(maitriserSoft.getCandidat().getIdentifiant());
        maitriserSoft.getMaitriserSoftPK().setSoftware(maitriserSoft.getSoftware1().getSoftware());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Software software1 = maitriserSoft.getSoftware1();
            if (software1 != null) {
                software1 = em.getReference(software1.getClass(), software1.getSoftware());
                maitriserSoft.setSoftware1(software1);
            }
            em.persist(maitriserSoft);
            if (software1 != null) {
                software1.getMaitriserSoftCollection().add(maitriserSoft);
                software1 = em.merge(software1);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findMaitriserSoft(maitriserSoft.getMaitriserSoftPK()) != null) {
                throw new PreexistingEntityException("MaitriserSoft " + maitriserSoft + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(MaitriserSoft maitriserSoft) throws NonexistentEntityException, Exception {
        maitriserSoft.getMaitriserSoftPK().setIdentifiant(maitriserSoft.getCandidat().getIdentifiant());
        maitriserSoft.getMaitriserSoftPK().setSoftware(maitriserSoft.getSoftware1().getSoftware());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            MaitriserSoft persistentMaitriserSoft = em.find(MaitriserSoft.class, maitriserSoft.getMaitriserSoftPK());
            Software software1Old = persistentMaitriserSoft.getSoftware1();
            Software software1New = maitriserSoft.getSoftware1();
            if (software1New != null) {
                software1New = em.getReference(software1New.getClass(), software1New.getSoftware());
                maitriserSoft.setSoftware1(software1New);
            }
            maitriserSoft = em.merge(maitriserSoft);
            if (software1Old != null && !software1Old.equals(software1New)) {
                software1Old.getMaitriserSoftCollection().remove(maitriserSoft);
                software1Old = em.merge(software1Old);
            }
            if (software1New != null && !software1New.equals(software1Old)) {
                software1New.getMaitriserSoftCollection().add(maitriserSoft);
                software1New = em.merge(software1New);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                MaitriserSoftPK id = maitriserSoft.getMaitriserSoftPK();
                if (findMaitriserSoft(id) == null) {
                    throw new NonexistentEntityException("The maitriserSoft with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(MaitriserSoftPK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            MaitriserSoft maitriserSoft;
            try {
                maitriserSoft = em.getReference(MaitriserSoft.class, id);
                maitriserSoft.getMaitriserSoftPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The maitriserSoft with id " + id + " no longer exists.", enfe);
            }
            Software software1 = maitriserSoft.getSoftware1();
            if (software1 != null) {
                software1.getMaitriserSoftCollection().remove(maitriserSoft);
                software1 = em.merge(software1);
            }
            em.remove(maitriserSoft);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<MaitriserSoft> findMaitriserSoftEntities() {
        return findMaitriserSoftEntities(true, -1, -1);
    }

    public List<MaitriserSoft> findMaitriserSoftEntities(int maxResults, int firstResult) {
        return findMaitriserSoftEntities(false, maxResults, firstResult);
    }

    private List<MaitriserSoft> findMaitriserSoftEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(MaitriserSoft.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public MaitriserSoft findMaitriserSoft(MaitriserSoftPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(MaitriserSoft.class, id);
        } finally {
            em.close();
        }
    }

    public int getMaitriserSoftCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<MaitriserSoft> rt = cq.from(MaitriserSoft.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
